import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/news/data/today_news_list_item_data.dart';
import 'package:sui_shou_pai/news/today/today_news_list_item.dart';

class TodayNewsList extends StatefulWidget {
  TodayNewsList({
    Key key,
  }) : super(key: key);

  @override
  TodayNewsListState createState() => TodayNewsListState();
}

class TodayNewsListState extends State<TodayNewsList> {
  List<dynamic> tempList = [];
  List<TodayNewsListItemData> todayNewsList = [];
  String myKey = "7e36bbbb830850025bb2691f4766e77c";

  @override
  void initState() {
    super.initState();
    Future.delayed(
        Duration.zero,
        () => setState(() {
              initData();
            }));
  }

  initData() async {
    HttpRequest.request("http://api.tianapi.com/internet/index",
            method: "GET",
            params: {'key': myKey, 'num': 10},
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      setState(() {
        tempList = json.decode(value.toString())['newslist'];
      });
      for (int i = 0; i < tempList.length; i++) {
        todayNewsList.add(TodayNewsListItemData.fromMap(tempList[i]));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: todayNewsList.length,
      itemBuilder: (BuildContext context, int index) {
        return initTodayNewsListItem(context, todayNewsList[index]);
      },
    );
  }
}
